package br.ucb.cm.config.page.piece.ui;

import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.ImageIcon;

import br.ucb.cm.board.util.ImageEnum;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.border.BevelBorder;

public class IconPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private final IconSpinner spinner;
	private JButton iconBtn;
	private JPanel panelSpinner;
	private JLabel lblName;
	private JButton btnPrev;
	private JButton btnNext;

	public IconPanel() {
		spinner = new IconSpinner();
		setBorder(new TitledBorder(null, "\u00CDcone", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		initComponents();
	}

	private void initComponents() {
		panelSpinner = new JPanel();

		iconBtn = new JButton();
		lblName = new JLabel("Nome");
		iconBtn.setBorder(new LineBorder(Color.RED));
		iconBtn.setBounds(40, 0, 64, 64);
		iconBtn.setMaximumSize(new Dimension(64, 64));
		iconBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				iconBtn.setIcon(spinner.next());
				lblName.setText(spinner.getCurrentIcon().getName());
			}
		});
		iconBtn.setIcon(spinner.get());
		lblName.setText(spinner.getCurrentIcon().getName());

		lblName.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lblName.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblName.setHorizontalAlignment(SwingConstants.CENTER);

		GroupLayout layout = new GroupLayout(this);
		layout.setHorizontalGroup(
				layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap(34, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblName, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
								.addComponent(panelSpinner, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
								.addContainerGap(35, Short.MAX_VALUE))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap(50, Short.MAX_VALUE)
						.addComponent(lblName, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panelSpinner, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(74, Short.MAX_VALUE))
				);
		this.setLayout(layout);
		panelSpinner.setLayout(null);
		panelSpinner.add(iconBtn);

		btnPrev = new JButton("");
		btnPrev.setIcon(new ImageIcon(IconPanel.class.getResource("/br/ucb/cm/config/resources/control_left.png")));
		btnPrev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				iconBtn.setIcon(spinner.prev());
				lblName.setText(spinner.getCurrentIcon().getName());
			}
		});
		btnPrev.setBounds(0, 0, 40, 64);
		panelSpinner.add(btnPrev);

		btnNext = new JButton("");
		btnNext.setIcon(new ImageIcon(IconPanel.class.getResource("/br/ucb/cm/config/resources/control_right.png")));
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				iconBtn.setIcon(spinner.next());
				lblName.setText(spinner.getCurrentIcon().getName());
			}
		});
		btnNext.setBounds(104, 0, 40, 64);
		panelSpinner.add(btnNext);
	}

	class IconSpinner{
		private int index;
		private List<ImageEnum> icons;

		public IconSpinner() {
			index = 0;
			icons = ImageEnum.getStandardIcons();
		}

		public ImageEnum getCurrentIcon(){
			return icons.get(index);
		}

		public ImageIcon get(){
			return new ImageIcon(IconPanel.class.getResource(icons.get(index).getResourcePath())); 
		}

		public ImageIcon next(){
			if(index < icons.size() - 1)
				index++;
			else
				index = 0;

			return get();
		}

		public ImageIcon prev(){
			if(index > 0)
				index--;
			else
				index = icons.size() - 1;

			return get();
		}

	}

	public ImageEnum getSelectedIcon(){
		return spinner.getCurrentIcon();
	}

	public void reset(){
		spinner.index = 0;
		iconBtn.setIcon(spinner.get());
		lblName.setText(spinner.getCurrentIcon().getName());
	}
}
